package listbyorder.access101_200.test149;

import java.math.BigInteger;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/12 10:33
 */
public class Solution1 {

    // 找出数组中在一条线上的最大的点数
    public int maxPoints(int[][] points) {
        if (points.length < 3) return points.length;
        int i = 0;
        for (; i < points.length - 1; i++) {
            // 如果任意的两个节点不在同一条直线上面，则直接break
            if (points[i][0] != points[i + 1][0] || points[i][1] != points[i + 1][1]) {
                break;
            }
        }
        if (i == points.length - 1) {
            return points.length;
        }
        int max = 0;
        for (int j = 0; j < points.length; j++) {
            for (int k = j + 1; k < points.length; k++) {
                if (points[j][0] == points[k][0] && points[j][1] == points[k][1]) {
                    continue;  // 如果前后的两个点完全一样则直接跳过
                }
                int tempMax = 0;
                for (int l = 0; l < points.length; l++) {
                    if (l != j && l != k) {
                        if (test2(points[j], points[k], points[l])) {
                            tempMax++;
                        }
                    }
                }
                if (tempMax > max) {
                    max = tempMax;
                }
            }
        }
        return max + 2;
    }

    // 判断三个点在不在一条直线上
    private boolean test1(int[] x, int[] y, int[] p) {
        return (long)(y[1] - x[1]) * (p[0] - x[0]) == (long)(p[1] - x[1]) * (y[0] - x[0]);
    }

    private boolean test2(int[] x, int[] y, int[] p) {
        BigInteger x0 = BigInteger.valueOf(x[0]);
        BigInteger x1 = BigInteger.valueOf(x[1]);
        BigInteger y0 = BigInteger.valueOf(y[0]);
        BigInteger y1 = BigInteger.valueOf(y[1]);
        BigInteger p0 = BigInteger.valueOf(p[0]);
        BigInteger p1 = BigInteger.valueOf(p[1]);
        return y1.subtract(x1).multiply(p0.subtract(x0)).equals(p1.subtract(x1).multiply(y0.subtract(x0)));
    }
}
